<?php
/* 
* [***********************]
* @author: mazhenyu[houdunwangmzy@163.com]
* @Date:   2014-06-23 17:11:29
* @Last Modified time: 2014-06-28 12:12:54
*/
class HtmlTag{
	public $tag = array(
		//设置block标签为块标签
		'channel' => array('block' => 1),
		'nav'	  => array('block' => 1),
		'arclist' => array('block' => 1),
		'tag'	  => array('block' => 1)
	);

	public function _tag($attr, $content, $parse){
		$row = isset($attr['row']) ? $attr['row'] : 0;
		$cid = isset($attr['cid']) ? $attr['cid'] : ($_GET['cid'] ? $_GET['cid'] : 0);
		if($cid) $where = 'category_cid=' . $cid;

		$str = <<<str
<?php 
\$tag = K('ArticleTagView')->where('{$where}')->group('tid')->all();
// p(\$tag);

?>
<foreach from="\$tag" value="\$field">
<?php
	\$field['url'] = U('List/index',array('tid'=>\$field['tid']));
?>
{$content}
</foreach>
str;
		return $str;

	}

	public function _arclist($attr, $content, $parse){
		//必须不在回收站
		$where = 'is_recycle=0';
		//所取行数
		$row = isset($attr['row']) ? $attr['row'] : NULL;
		//文章属性
		$attribute = isset($attr['attr']) ? $attr['attr'] : NULL;
		//如果有置顶或者推荐属性
		if($attribute) $where .= ' and attr like "%'.$attribute.'%"';
		//标题长度
		$titlelen = isset($attr['titlelen']) ? $attr['titlelen'] : 0;
		//分类id
		if(isset($attr['cid'])){
			$cid = $attr['cid'];
		}else if($getCid = Q('get.cid',0,'intval')){
			$cid = $getCid;
		}else{
			$cid = 0;
		}
		if($cid) $where .= ' and category_cid=' . $cid;
		

		//处理标签云
		$tid = isset($_GET['tid']) ? $_GET['tid'] : 0;
		//如果有tid
		if($tid){
			$aidArr = K('ArticleTag')->get_data(array('tag_tid'=>$tid),'article_aid');
			$temp = array();
			foreach ($aidArr as $v) {
				$temp[] = $v['article_aid'];
			}

			$where .= ' and aid in (' . implode(',', $temp) . ')';

		}

		

		//分页
		$pagenum = isset($attr['pagenum']) ? $attr['pagenum'] : 0;
		$pagerows = isset($attr['pagerows']) ? $attr['pagerows'] : 10;



		$str = <<<str
<?php 
if($pagenum){
		if(isset(\$_GET['cid'])){
			\$cateInfo = K('Category')->where(array('cid'=>\$_GET['cid']))->field('is_listhtml,htmldir')->find();
			if(\$cateInfo['is_listhtml']){
				//如果是静态则处理静态地址
				Page::\$staticUrl = '__ROOT__/Static/'.\$cateInfo['htmldir'].'/{page}.html';
				p(Page::\$staticUrl);die;
			}
	}
	
	\$arcTotal = K('ArticleCateView')->where('{$where}')->count();
	\$pageObj = new Page(\$arcTotal,$pagerows,$pagenum);
	//如果存在cid则处理静态地址
	\$page = \$pageObj->show();
	\$arc = K('ArticleCateView')->where('{$where}')->limit({$row})->all(\$pageObj->limit());
}else{
	\$arc = K('ArticleCateView')->where('{$where}')->limit({$row})->all();
}

?>
<foreach from="\$arc" value="\$field">
<?php 
	\$field['url'] = get_arc_url(\$field['is_archtml'],\$field['htmldir'],\$field['aid']);
	if($titlelen) \$field['title'] = mb_substr(\$field['title'], 0,$titlelen,'utf-8');
	\$field['thumb'] = '__ROOT__/' . \$field['thumb'];
	\$field['time'] = date('y-m-d',\$field['sendtime']);
	\$field['total'] = K('Comment')->get_total(\$field['aid']);
?>
{$content}
</foreach>

str;
	return $str;

	}

		public function _nav($attr, $content, $parse){
		//分类必须显示
		$where = 'is_show=1 and is_nav=1';
		//属性cid
		$cid = isset($attr['cid']) ? $attr['cid'] : 0;
		//属性类型
		$type = isset($attr['type']) ? $attr['type'] : NULL;
		//几行
		$row = isset($attr['row']) ? $attr['row'] : NULL;
		//排序
		$order = isset($attr['order']) ? $attr['order'] : 'csort DESC';
		$class = isset($attr['class']) ? $attr['class'] : '';


		switch ($type) {
			case 'self':
				$pid = K('Category')->get_field($cid,'pid');
				$where .= ' and pid=' . $pid;
				break;
			
			default:
				# code...
				break;
		}
		$str = <<<str
<?php \$cate = K('Category')->where('{$where}')->limit({$row})->order('{$order}')->all(); ?>
<foreach from="\$cate" value="\$field">
<?php 
\$field['curl'] = get_cate_url(\$field['is_listhtml'],\$field['htmldir'],\$field['cid']);
\$field['class'] = \$field['cid'] == Q('get.cid') ? $class : '';
?>
{$content}
</foreach>
str;
		return $str;

	}

	public function _channel($attr, $content, $parse){
		//分类必须显示
		$where = 'is_show=1';

		//属性cid
		$cid = $attr['cid'];
		//属性类型
		$type = $attr['type'];

		switch ($type) {
			case 'self':
				$pid = K('Category')->get_field($cid,'pid');
				$where .= ' and pid=' . $pid;
				break;
			
			default:
				# code...
				break;
		}
		$str = <<<str
<?php \$cate = K('Category')->where('{$where}')->all(); ?>
<foreach from="\$cate" value="\$field">

{$content}
</foreach>
str;
		return $str;

	}
}
?>
